//
//  YDBBannerControl.swift
//  BaseFramework
//
//  Created by 杨东宝 on 2020/5/8.
//  Copyright © 2020 杨东宝. All rights reserved.
//

import UIKit

class YDBBannerControl: UIPageControl {
    
    var currentImage:UIImage?
    var inactiveImage:UIImage?
    var currentImageSize:CGSize?
    var inactiveImageSize:CGSize?
    var param:YDBBannerParam?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    convenience init(frame:CGRect,aparam:YDBBannerParam){
        self.init(frame: frame)
        param = aparam
        isUserInteractionEnabled = false
        hidesForSinglePage = true
        currentPageIndicatorTintColor = param?.wBannerControlSelectColor
        pageIndicatorTintColor = param?.wBannerControlColor
        
        if (param?.wBannerControlImage) != nil {
            
            inactiveImage = UIImage.init(named: param?.wBannerControlImage ?? "")
            inactiveImageSize = param?.wBannerControlImageSize;
            pageIndicatorTintColor = UIColor.clear
            
        }
        if (param?.wBannerControlSelectImage) != nil {
            
            currentImage = UIImage.init(named: param?.wBannerControlImage ?? "")
            currentImageSize = param?.wBannerControlImageSize;
            currentPageIndicatorTintColor = UIColor.clear
            
        }
        
        self.resetFrame()
        
    }
    
    
    override var currentPage: Int{
        
        set{
            super.currentPage = newValue
        }
        get{
            super.currentPage
        }
        
    }
    
    func updateDots() {
        
        for i in 0..<self.subviews.count {
            
            let dot:UIImageView? = self.imageViewForSubview(view: self.subviews[i], currPage: i)
            
            if i == currentPage {
                dot?.image = self.currentImage
                var  rect:CGRect = dot?.frame ?? CGRect(x: 0, y: 0, width: 0, height: 0)
                rect.size = self.currentImageSize ?? CGSize(width: 0, height: 0)
                dot?.frame = rect
                dot?.layer.masksToBounds = true
                dot?.layer.cornerRadius = self.param?.wBannerControlImageRadius ?? (self.currentImageSize?.height ?? 0)/2
            }else{
                
                dot?.image = self.inactiveImage;
                var rect:CGRect  = dot?.frame ?? CGRect();
                rect.size = inactiveImageSize ?? CGSize()
                dot?.frame = rect;
                dot?.layer.masksToBounds = true;
                dot?.layer.cornerRadius = self.param?.wBannerControlImageRadius ?? (self.currentImageSize?.height ?? 0)/2
                
            }
        }
        
    }
    
    func imageViewForSubview(view:UIView,currPage:Int) -> UIImageView? {
        
        var dot:UIImageView? = nil
        
        if view.isKind(of: UIView.self) {
            
            for subView in view.subviews {
                
                if subView.isKind(of: UIImageView.self) {
                    
                    dot = subView as? UIImageView
                }
            }
            
            
            if (dot == nil) {
                dot = UIImageView.init(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))
                
                view.addSubview(dot!)
            }
            
        }else{
            
            dot = view as? UIImageView;
            
        }
        return dot
    }
    
    func resetFrame()  {
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        if let _ = self.param?.wBannerControlImage,let _ = self.param?.wBannerControlSelectImage{
            
            var tmp:UIImageView?
            
            for i in 0..<self.subviews.count {
                
                let dot: UIImageView = self.subviews[i] as! UIImageView
                
                var x:CGFloat = 0
                if let t = tmp {
                    
                    x = t.frame.maxX+CGFloat(self.param!.wBannerControlSelectMargin)
                }else{
                    
                    x = CGFloat(self.param!.wBannerControlSelectMargin)
                }
                
                var y:CGFloat  = 0;
                if i == self.currentPage {
                    y = self.bounds.size.height -  (self.currentImageSize?.height ?? 0)/2
                    
                    dot.frame = CGRect(x:x, y:y,width: self.currentImageSize?.width ?? 0,height: self.currentImageSize?.height ?? 0)
                }else {
                    y = self.bounds.size.height - (self.inactiveImageSize?.height ?? 0)/2
                    dot.frame = CGRect(x:x, y:y,width: self.inactiveImageSize?.width ?? 0,height: self.inactiveImageSize?.height ?? 0)
                }
                tmp = dot
                
                if i == self.subviews.count-1 {
                    var rect: CGRect = self.frame;
                    rect.size.width = dot.frame.maxX
                    rect.origin.x = (self.param?.wFrame?.size.width ?? 0 - rect.size.width)/2;
                    self.frame = rect;
                }
                
            }
            self.resetFrame()
        }
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
